package com.wsq.ui;

import android.content.Context;
import android.content.DialogInterface;
import android.preference.DialogPreference;
import android.util.AttributeSet;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;

import com.wsq.calculate.R;
import com.wsq.global.Configuration;

public class FeedbackDialogPreference extends DialogPreference implements
	OnSeekBarChangeListener, OnClickListener {
    public void onClick(DialogInterface paramDialogInterface, int paramInt) {
	super.onClick(paramDialogInterface, paramInt);
	if ((paramInt == -1) && (shouldPersist())) {
	    // Click the positive Button
	    persistInt(progress);
	}
    }

    @Override
    protected void onBindView(View view) {
	// TODO Auto-generated method stub
	play = (ImageView) view.findViewById(R.id.idSettingPlay);
	play.setOnClickListener(this);
	super.onBindView(view);
    }

    @Override
    protected void onBindDialogView(View view) {
	// TODO Auto-generated method stub
	mBindPlay = (ImageView) view.findViewById(R.id.idBindPlay);
	mBindPlay.setOnClickListener(this);
	mTime = (TextView) view.findViewById(R.id.idBindDialogText);
	mSeekBar = (SeekBar) view.findViewById(R.id.idBindDialogSeekbar);
	progress = getPersistedInt(45);
	mTime.setText(progress + "ms");
	mSeekBar.setMax(200);
	mSeekBar.setProgress(progress);
	mSeekBar.setOnSeekBarChangeListener(this);
	super.onBindDialogView(view);
    }

    private Context mContext;

    private TextView mTime;
    private ImageView mBindPlay;
    private SeekBar mSeekBar;
    private ImageView play;
    private int progress;

    public FeedbackDialogPreference(Context context, AttributeSet attrs) {
	super(context, attrs);
	// TODO Auto-generated constructor stub
	this.mContext = context;
    }

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress,
	    boolean fromUser) {
	this.progress = progress;
	if (progress != 0)
	    mTime.setText(progress + "ms");
	else
	    mTime.setText("off");
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {
	// TODO Auto-generated method stub

    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
	// TODO Auto-generated method stub

    }

    @Override
    public void onClick(View v) {
	// TODO Auto-generated method stub
	switch (v.getId()) {
	case R.id.idBindPlay:
	    Configuration.instance().vibrate(mContext, progress);
	    break;
	case R.id.idSettingPlay:
	    progress = getPersistedInt(45);
	    Configuration.instance().vibrate(mContext, progress);
	    break;
	}
    }

}
